from flask import request, jsonify
from user import userservice
from payment.wechat_pay_service import wechat_pay_service

def init_payment_routes(app):
    """
    初始化支付相关路由
    """
    
    @app.route('/payment/products', methods=['GET'])
    def get_products():
        """
        获取产品列表
        """
        product_type = request.args.get('productType')
        try:
            result = wechat_pay_service.get_products(product_type)
            return jsonify(result)
        except Exception as e:
            return jsonify({
                'success': False,
                'error': '获取产品列表失败',
                'message': str(e)
            }), 500

    @app.route('/payment/vip-info', methods=['GET'])
    def get_vip_info():
        """
        获取用户VIP信息
        """
        source = request.args.get('source')
        source_id = request.args.get('source_id')
        user_id = userservice.get_user_by_source_id(source_id, source)
        if user_id is None:
            return jsonify({
                'error': '请先登录',
                'message': '请先登录, 联系管理员'
            }), 400

        try:
            result = wechat_pay_service.get_user_vip_info(user_id)
            return jsonify(result)
        except Exception as e:
            return jsonify({
                'success': False,
                'error': '获取VIP信息失败',
                'message': str(e)
            }), 500

    @app.route('/payment/create-order', methods=['POST'])
    def create_payment_order():
        """
        创建支付订单
        """
        source = request.args.get('source')
        source_id = request.args.get('source_id')
        user_id = userservice.get_user_by_source_id(source_id, source)
        if user_id is None:
            return jsonify({
                'error': '请先登录',
                'message': '请先登录, 联系管理员'
            }), 400

        data = request.get_json()
        if not data or 'product_id' not in data:
            return jsonify({
                'error': '请提供产品ID',
                'example': {
                    'product_id': 1
                }
            }), 400

        product_id = data['product_id']
        
        # 获取用户openid（这里需要根据实际情况获取）
        openid = source_id  # 假设source_id就是openid

        try:
            result = wechat_pay_service.create_order(user_id, product_id, openid)
            return jsonify(result)
        except Exception as e:
            return jsonify({
                'success': False,
                'error': '创建支付订单失败',
                'message': str(e)
            }), 500

    @app.route('/payment/notify', methods=['POST'])
    def payment_notify():
        """
        微信支付回调接口
        """
        try:
            notify_data = request.get_json()
            result = wechat_pay_service.verify_payment_notify(notify_data)
            return jsonify(result)
        except Exception as e:
            return jsonify({
                'success': False,
                'error': '处理支付回调失败',
                'message': str(e)
            }), 500 